package com.rightsidetech.http.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2WebMvc
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("admin")
                .apiInfo(apiAdminInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.rightsidetech.http.controller.admin"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemesAdmin());
    }


    @Bean
    public Docket userAppRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("userApp")
                .apiInfo(userAppApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.rightsidetech.http.controller.userApp"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemesUserApp());
    }


    @Bean
    public Docket agentAdminRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("agentAdmin")
                .apiInfo(agentAdminApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.rightsidetech.http.controller.agentAdmin"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemesAgentAdmin());
    }

    @Bean
    public Docket storeAppRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("storeApp")
                .apiInfo(storeAppApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.rightsidetech.http.controller.storeApp"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemesStoreApp());
    }

    @Bean
    public Docket agentAppRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("agentApp")
                .apiInfo(agentAppApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.rightsidetech.http.controller.agentApp"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemesAgentApp());
    }


    private ApiInfo apiAdminInfo() {
        return new ApiInfoBuilder().title("后台业务接口").description("更多商城信息,请关注：http://www.rightsidetech.com/").termsOfServiceUrl("https://192.168.1.198:8118/doc.html")
                .version("1.0")
                .build();
    }
    private ApiInfo userAppApiInfo() {
        return new ApiInfoBuilder().title("用户app业务接口").description("更多电动车信息,请关注：http://www.rightsidetech.com/")
                .termsOfServiceUrl("https://192.168.1.198:8118/doc.html")
                .version("1.0")
                .build();
    }
    private ApiInfo agentAdminApiInfo() {
        return new ApiInfoBuilder().title("代理商后台业务接口").description("更多电动车信息,请关注：http://www.rightsidetech.com/")
                .termsOfServiceUrl("https://192.168.1.198:8118/doc.html")
                .version("1.0")
                .build();
    }
    private ApiInfo storeAppApiInfo() {
        return new ApiInfoBuilder().title("门店app业务接口").description("更多电动车信息,请关注：http://www.rightsidetech.com/")
                .termsOfServiceUrl("https://192.168.1.198:8118/doc.html")
                .version("1.0")
                .build();
    }
    private ApiInfo agentAppApiInfo() {
        return new ApiInfoBuilder().title("代理商后台业务接口").description("更多电动车信息,请关注：http://www.rightsidetech.com/")
                .termsOfServiceUrl("https://192.168.1.198:8118/doc.html")
                .version("1.0")
                .build();
    }

    private List<SecurityScheme> securitySchemesAdmin() {
        ArrayList<SecurityScheme> list = new ArrayList<>();
        list.add(new ApiKey("token", "token_admin", "header"));
        return list;
    }
    private List<SecurityScheme> securitySchemesUserApp() {
        ArrayList<SecurityScheme> list = new ArrayList<>();
        list.add(new ApiKey("token", "token_userApp", "header"));
        return list;
    }
    private List<SecurityScheme> securitySchemesAgentAdmin() {
        ArrayList<SecurityScheme> list = new ArrayList<>();
        list.add(new ApiKey("token", "token_agentAdmin", "header"));
        return list;
    }
    private List<SecurityScheme> securitySchemesStoreApp() {
        ArrayList<SecurityScheme> list = new ArrayList<>();
        list.add(new ApiKey("token", "token_storeApp", "header"));
        return list;
    }


    private List<SecurityScheme> securitySchemesAgentApp() {
        ArrayList<SecurityScheme> list = new ArrayList<>();
        list.add(new ApiKey("token", "token_agentApp", "header"));
        return list;
    }

}
